import Mock from 'mockjs'
const rankingListMock = require('../../mock/ranking-list');
const rankingListMockTop3 = require('../../mock/ranking-list-top3');
const gameListMock = require('../../mock/game-list');
const gameDetailMock = require('../../mock/game-detail');
const myGameDetailMock = require('../../mock/my-game-detail');

export default{
	data(){
		return {
			page: 0, //页码
			pageNum: 6, //每页加载数据量
			loadingType: 1, //0加载前 1加载中 2没有更多
			isLoading: false, //刷新数据
			loaded: false, //加载完毕
		}
	},
	methods: {
		log(data){
			console.log(JSON.parse(JSON.stringify(data)))
		},
		
		
		/**
		 * navigatorTo跳转页面
		 * @param {String} url
		 * @param {Object} options
		 * @param {Boolean} options.login 是否检测登录  
		 */
		navTo(url, options={}){
			console.log("navTo>>"+url)
			this.$util.throttle(()=>{
				if(!url){
					return;
				}
				if((~url.indexOf('login=1') || options.login) && !this.$store.getters.hasLogin){
					url = '/pagesA/auth/login';
				}
				uni.navigateTo({
					url
				})
			}, 300)
		},
		$post_mock(module, operation, data={}, ext={}){
			this.request_mock(module, operation, data, ext)
		},
		/**
		 * $request云函数请求
		 * @param {String} module
		 * @param {String} operation
		 * @param {Boolean} data 请求参数
		 * @param {Boolean} ext 附加参数
		 * @param {Boolean} ext.showLoading 是否显示loading状态，默认不显示
		 * @param {Boolean} ext.hideLoading 是否关闭loading状态，默认关闭
		 * @param {Boolean} ext.login 未登录拦截
		 * @param {Boolean} ext.setLoaded 加载完成是设置页面加载完毕
		 */
		$request_mock(module, operation, data={}, ext={}){
			let that = this;
			console.log("request_mock module:"+JSON.stringify(module)+", operation:"+operation+",data:"+JSON.stringify(data)+",ext:"+JSON.stringify(ext))
			// console.log(api[operation]);
			if(ext.login && !this.$util.isLogin()){
				return;
			}
			if(ext.showLoading){
				this.isLoading = true;
			}
			switch (operation) {
				case 'RankingList':
					return rankingListMock()
					break;
				case 'RankingListTop3':
					return rankingListMockTop3()
					break;
				case 'GameList':
					return gameListMock()
					break;
				case 'GameDetail':
					return gameDetailMock()
					break;
				case 'MyGameDetail':
					return myGameDetailMock()
					break;
					
				default:
					return {
						mockFlag: false,
					}
					break;
			}
			// return new Promise((resolve, reject)=> {
			// 	util.request(api[operation], data).then(function(res) {
			// 		// console.log("response >> "+JSON.stringify(res));
			// 		if(res.status === 500){
			// 			that.navTo('/pagesA/auth/login');
			// 		}
			// 		if(ext.hideLoading !== false){
			// 			that.isLoading = false;
			// 		}
			// 		setTimeout(()=>{
			// 			if(that.setLoaded !== false){
			// 				that.loaded = true;
			// 			}
			// 		}, 100)
			// 		that.$refs.confirmBtn && that.$refs.confirmBtn.stop();
			// 		resolve(res);
			// 	}).catch((err) => {
			// 		reject(err);
			// 	});
			// })
		},
		imageOnLoad(data, key){
			setTimeout(()=>{
				this.$set(data, 'loaded', true);
			}, 100)
		},
		showPopup(key){
			this.$util.throttle(()=>{
				this.$refs[key].open();
			}, 200)
		},
		hidePopup(key){
			this.$refs[key].close();
		},
		stopPrevent(){},
	},
}